package com.nativetalk.service.three.live.insert;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.coupon.TdNtCouponDetail;
import com.nativetalk.bean.live.TdNtLiveCourseHisDetails;
import com.nativetalk.bean.live.TdNtLiveCourseHisInsert;
import com.nativetalk.bean.live.TdNtLiveStudyDetail;
import com.nativetalk.bean.live.TdNtLiveStudyInsert;
import com.nativetalk.bean.member.TdNtMemberDetail;
import com.nativetalk.bean.purchase.TdNtMembPurchaseChange;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.CommonUtil;
import com.nativetalk.dao.*;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lizhun on 16/8/12.
 */
@Service
public class ThreeLiveInsertServiceImpl implements  ThreeLiveInsertService {
    private static Logger logger = Logger.getLogger(ThreeLiveInsertServiceImpl.class);
    @Resource
    private TdNtLiveCourseHisDao tdNtLiveCourseHisDao;
    @Resource
    private TdNtLiveStudyDao tdNtLiveStudyDao;
    @Resource
    private TdNtCouponDao tdNtCouponDao;
    @Resource
    private TdNtMemberDao tdNtMemberDao;
    @Resource
    private TdNtMembPurchaseChangeDao tdNtMembPurchaseChangeDao;
    @Override
    public RetInfo addLive(TdNtLiveStudyInsert tdNtLiveStudyInsert) {
        String logInfo = this.getClass().getName() + ":addLive:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            TdNtLiveCourseHisDetails tdNtLiveCourseHisDetails = tdNtLiveCourseHisDao.findTdNtLiveCourseHisDetails(tdNtLiveStudyInsert.getLive_cou_his_id());
            Map<String,Object> result = new HashMap<>();
            /**
             * 判断直播是否可购买
             */
            if (tdNtLiveCourseHisDetails!=null && !(tdNtLiveCourseHisDetails.getLive_cou_state().equals("3"))
                    && !(tdNtLiveCourseHisDetails.getLive_cou_state().equals("4"))) {
                /**
                 * 判断是否购买过该直播
                 */
                Map<String, Integer> params = new HashMap<>();
                params.put("memb_id",tdNtLiveStudyInsert.getMemb_id());
                params.put("live_cou_his_id",tdNtLiveStudyInsert.getLive_cou_his_id());
                TdNtLiveStudyDetail tdNtLiveStudyDetail = tdNtLiveStudyDao.findTdNtLiveStudyDetail(params);
                if (tdNtLiveStudyDetail != null) {
                    retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                    retInfo.setTip("已购买该直播");
                } else {

                    if (tdNtLiveCourseHisDetails.getLive_cou_system_amount().compareTo(new BigDecimal(0))==0) {
                        tdNtLiveStudyInsert.setState("2");
                    } else {
                        /**
                         * 判断用户是否有该直播优惠券
                         */
                        BigDecimal coupon_amount = new BigDecimal(0);
                        TdNtCouponDetail detail = new TdNtCouponDetail();
                        detail.setMemb_id(tdNtLiveStudyInsert.getMemb_id());
                        detail.setState(1);
                        detail.setType(1);
                        detail.setLimit_id(tdNtLiveStudyInsert.getLive_cou_his_id());
                        TdNtCouponDetail couponDetail = tdNtCouponDao.findTdNtCouponDetail(detail);
                        if (couponDetail != null &&
                                (couponDetail.getCoupon_limit_amount().compareTo(new BigDecimal(0)) == 0
                                        || couponDetail.getCoupon_limit_amount().compareTo(tdNtLiveCourseHisDetails.getLive_cou_system_amount()) > -1
                                )
                                ) {
                            coupon_amount = couponDetail.getCoupon_amount();
                            if (tdNtLiveCourseHisDetails.getLive_cou_system_amount().compareTo(couponDetail.getCoupon_amount()) < 0) {
                                coupon_amount = tdNtLiveCourseHisDetails.getLive_cou_system_amount();
                            }
                            /**
                             * 更新优惠券
                             */
                            couponDetail.setState(2);
                            tdNtCouponDao.updateTdNtCoupon(couponDetail);
                        }
                        if (tdNtLiveCourseHisDetails.getLive_cou_system_amount().compareTo(coupon_amount) <= 0) {
                            tdNtLiveStudyInsert.setState("2");
                        } else {
                            tdNtLiveStudyInsert.setState("2");
                            BigDecimal change_amount = tdNtLiveCourseHisDetails.getLive_cou_system_amount().subtract(coupon_amount);
                            /**
                             * 添加资金变动记录
                             */
                            TdNtMemberDetail tdNtMember = tdNtMemberDao.findTdNtMemberDetail(tdNtLiveStudyInsert.getMemb_id());
                            if (tdNtMember.getMemb_purchase_balance().compareTo(change_amount) > -1) {
                                TdNtMembPurchaseChange tdNtMembPurchaseChange = new TdNtMembPurchaseChange();
                                tdNtMembPurchaseChange.setMemb_id(tdNtMember.getMemb_id());
                                tdNtMembPurchaseChange.setMfchg_change_amount(change_amount);
                                tdNtMembPurchaseChange.setMfchg_change_month(CommonUtil.getMonth(new Date()));
                                tdNtMembPurchaseChange.setMfchg_change_name("购买公开课");
                                tdNtMembPurchaseChange.setMfchg_change_type(Constants.FundChangeType.wallet_out);
                                tdNtMembPurchaseChange.setState(Constants.PayState.success);
                                tdNtMembPurchaseChange.setMfchg_number(CommonUtil.getOrderNub());
                                tdNtMembPurchaseChange.setMfchg_back_amount(tdNtMember.getMemb_purchase_balance().subtract(change_amount));
                                tdNtMembPurchaseChange.setMfchg_front_amount(tdNtMember.getMemb_purchase_balance());
                                tdNtMembPurchaseChangeDao.insertTdNtMembPurchaseChange(tdNtMembPurchaseChange);
                                tdNtMember.setMemb_purchase_balance(tdNtMember.getMemb_purchase_balance().subtract(change_amount));
                                tdNtMemberDao.updateMemberPurchase(tdNtMember);
                                tdNtLiveStudyInsert.setMfchg_id(tdNtMembPurchaseChange.getMfchg_id());
                            } else {
                                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                                retInfo.setTip(ValidationMessages.getValue("MEMBBALANCE.NO"));
                                return retInfo;
                            }
                        }
                    }

                    /**
                     * 添加直播
                     */

                    tdNtLiveStudyInsert.setState("2");
                    tdNtLiveStudyDao.addTdNtLiveStudyInsert(tdNtLiveStudyInsert);
                    retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                    retInfo.setTip("成功");
                    retInfo.setObj(result);
                }


            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("直播不可购买");
            }
            /**
             * 更新购买直播人数
             */
            TdNtLiveCourseHisInsert tdNtLiveCourseHisInsert = new TdNtLiveCourseHisInsert();
            tdNtLiveCourseHisInsert.setLive_cou_his_id(tdNtLiveCourseHisDetails.getLive_cou_his_id());
            tdNtLiveCourseHisInsert.setLive_cou_size(tdNtLiveCourseHisDetails.getLive_cou_size()+1);
            tdNtLiveCourseHisDao.updateTdNtLiveCourseHisSize(tdNtLiveCourseHisInsert);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
